package cc.rengu.igas.channel.alipay.core.service.trans;

import cc.rengu.igas.channel.alipay.common.constant.AlipayParamConstant;
import cc.rengu.igas.channel.alipay.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.alipay.core.model.request.CupsAlipayNoticeRequest;
import cc.rengu.igas.channel.alipay.core.service.base.NoticeService;
import cc.rengu.igas.share.facade.base.Header;
import cc.rengu.igas.share.facade.request.AlipayPayResultNoticeRequest;
import cc.rengu.igas.share.facade.response.AlipayPayResultNoticeResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;

public class CupsAlipayResultNoticeService extends NoticeService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        CupsAlipayNoticeRequest cupsAlipayNoticeRequest = new CupsAlipayNoticeRequest();
        ConvertUtil.convertOutput(cupsAlipayNoticeRequest);
        return cupsAlipayNoticeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        AlipayPayResultNoticeRequest alipayPayResultNoticeRequest = new AlipayPayResultNoticeRequest();
        ConvertUtil.convertOutput(alipayPayResultNoticeRequest);
        Header header = alipayPayResultNoticeRequest.getHeader();
        header.setTxnNum("T00SH003");
        header.setTraceNo(ChannelEnum.UPAL.getChannelType() + DateUtil.getCurrentDateTime("yyyyMMddHHmmssSSS") + RandomUtil.randomNumStr(10));
        header.setSrcSysId(ChannelEnum.UPAL.getChannelType());
        header.setChanlId("*");
        alipayPayResultNoticeRequest.setHeader(header);
        rglog.debug("打印支付宝交易结果通知报文<{}>", JSON.toJSONString(alipayPayResultNoticeRequest));
        DubboService dubboService = new DubboServiceImpl();
        Result<AlipayPayResultNoticeResponse> responseResult = (Result<AlipayPayResultNoticeResponse>) dubboService.callDubboService("alwxUnifiedService", "alipayPayResultNotice", alipayPayResultNoticeRequest);
        if (null == responseResult || !responseResult.isSuccess() || !"00000000".equals(responseResult.getResult().getRespCode())) {
            rglog.error("支付结果处理失败");
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, AlipayParamConstant.STRING_SUCCESS);
        } else {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, AlipayParamConstant.STRING_SUCCESS);
        }
        return null;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {

    }
}
